package com.youxin.foundation.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;
/**
 * String工具类
 * @author yingde.cao
 *
 */
public class StringUtils {
	public final static String NULL_STRING="\0";
	public final static String NEW_LINE="\r";
	/**
	 * 是否为数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){
		if(str==null){
			return false;
		}
		for (int i = 0; i < str.length(); i++){
			if (!Character.isDigit(str.charAt(i))){
				return false;
			}
		}
		return true;
	}
	
	public static boolean contains(String source,String contains)
	{
		return contains(source, contains, true);
	}
	
	public static boolean contains(String source,String contains,boolean ignoreCase)
	{
		if(source==null){
			return false;
		}
		if(ignoreCase)
		{
			return source.toLowerCase().indexOf(contains.toLowerCase())>=0;
		}else{
			return source.indexOf(contains)>=0;
		}
	}

	/**
	 * 是否为空白字符
	 * @param str
	 * @return
	 */
	public static boolean isWhiteSpace(String str){
		if(str==null){
			return true;
		}
		for (int i = 0; i < str.length(); i++){
			if (!Character.isWhitespace(str.charAt(i))){
				return false;
			}
		}
		return true;
	}
	
	public static String trim(String str)
	{
		if(str==null){
			return null;
		}
		return str.trim();
	}
	
	public static String trim(String str,char ch)
	{
		if(str==null){
			return "";
		}
		String result=trimLeft(str,ch);
		result=trimRight(result, ch);
		return result;
	}
	
	public static String trim(String str,String chs)
	{
		if(str==null){
			return "";
		}
		String result=trimLeft(str,chs);
		result=trimRight(result, chs);
		return result;
	}
	
	public static String trimLeft(String str,char ch)
	{
		if(str==null){
			return "";		
		}
		String result=str;
		while(result.length()>0&&result.charAt(0)==ch)
		{
			result=result.substring(1);			
		}
		return result;
	}
	
	public static String trimLeft(String str,String chs)
	{
		if(str==null){
			return "";
		}
		if (str.length()<chs.length()) {
			return str;
		}
		String result=str;
		while(result.length()>0&&result.indexOf(chs)==0)
		{
			result=result.substring(chs.length());
		}
		return result;
	}
	
	public static String trimLeftOne(String str,String chs)
	{
		if(str==null){
			return "";
		}
		if (str.length()<chs.length()) {
			return str;
		}
		String result=str;
		if(result.length()>0&&result.indexOf(chs)==0)
		{
			result=result.substring(chs.length());
		}
		return result;
	}
	
	public static String trimRight(String str,char ch)
	{
		if(str==null){
			return "";
		}
		String result=str;		
		while(result.length()>0&&result.charAt(result.length()-1)==ch)
		{
			result=result.substring(0,result.length()-1);
		}
		return result;
	}
	
	public static String trimRight(String str,String chs)
	{
		if(str==null){
			return "";
		}
		if (str.length()<chs.length()) {
			return str;
		}
		String result=str;		
		while(result.length()>0&&result.lastIndexOf(chs)==(result.length()-chs.length()))
		{
			result=result.substring(0,result.length()-chs.length());
		}
		return result;
	}
	
	public static String trimRightOne(String str,String chs)
	{
		if(str==null){
			return "";
		}
		if (str.length()<chs.length()) {
			return str;
		}
		String result=str;		
		if(result.length()>0&&result.lastIndexOf(chs)==(result.length()-chs.length()))
		{
			result=result.substring(0,result.length()-chs.length());
		}
		return result;
	}
	
	
	public static String trimLeftOne(String str,char ch)
	{
		if(str==null){
			return "";
		}
		
		String result=str;
		if(result.length()>0&&result.charAt(0)==ch)
		{
			result=result.substring(1);
		}
		return result;
	}
	
	public static String trimRightOne(String str,char ch)
	{
		if(str==null){
			return "";
		}
		
		String result=str;		
		if(result.length()>0&&result.charAt(result.length()-1)==ch)
		{
			result=result.substring(0,result.length()-1);
		}
		return result;
	}
	
	public static String guid()
	{		
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public static String escapeJSON(String jsonString) {
		if (jsonString==null) {
			return "";
		}
		jsonString=jsonString.replaceAll("'", "\\'");
		return jsonString;
	}
	
	public static InputStream stringToInputStream(String str) throws IOException{		
		return  new ByteArrayInputStream(str.getBytes());   
	}
	
	public static boolean isNullOrEmpty(String string)
	{
		return string==null||string.equals("");
	}
	
	public static String isNullSetEmpty(String string)
	{
		return string==null?"":string;
	}
	
	public static String isNullOrEmptySet(String string,String defalutValue)
	{
		if(isNullOrEmpty(string)){
			return defalutValue;
		}
		return string;
	}
	
	public static String substring(String source,int length)
	{
		if(source==null)
		{
			return "";
		}
		if(source.length()>length)
		{
			return source.substring(0, length);
		}
		return source;
	}
	
	public static String substring(String source,String prevStr)
	{
		return substring(source, prevStr, null, false);
	}
	
	public static String substring(String source,String prevStr,String endStr)
	{
		return substring(source, prevStr, endStr, false);
	}
	
	public static String substring(String source,String prevStr,String endStr,boolean ignoreCase)
	{
		if(source==null){
			return "";
		}
		if(ignoreCase)
		{
			source=source.toLowerCase(Locale.US);
			prevStr=prevStr.toLowerCase(Locale.US);
			if(endStr!=null)
			{
				endStr=endStr.toLowerCase(Locale.US);
			}
		}
		int prevIndex=source.indexOf(prevStr);
		if(prevIndex==-1)
		{
			return "";
		}
		int newStart=prevIndex+prevStr.length();
		if(newStart>=source.length()){
			return "";
		}
		
		if(endStr!=null)
		{
			int endIndex=source.indexOf(endStr, newStart);
			if(endIndex==-1){
				return "";
			}
			if(endIndex<=newStart)
			{
				return "";
			}
			return source.substring(newStart, endIndex);			
		}else{
			return source.substring(newStart);
		}
		
	}

	/**
	 * 数据转换为不同的进制
	 * @param n
	 * @param b
	 * @return 
	 */
	public static String digitToOther(int n, int b)
	{
		String str = "", digitChar = "0123456789abcdef";
		if (n == 0)
			return "";
		else
		{
			str = digitToOther(n/b, b);
			return str + digitChar.charAt(n % b);
		}
	}
	
	/** 
     * 判断非负数的整数或者携带一位或者两位的小数 
     *  
     * @function:   
     * @param obj  
     * @return boolean   
     * @exception  
     * @author: 
     * @since  1.0.0 
     */  
    public static boolean judgeTwoDecimal(Object obj){  
        boolean flag = false;  
        try {  
            if(obj != null){  
                String source = obj.toString();  
                // 判断是否是整数或者是携带一位或者两位的小数  
                Pattern pattern = Pattern.compile("^[+]?([0-9]+(.[0-9]{1,2})?)$");  
                if(pattern.matcher(source).matches()){  
                    flag = true;  
                }    
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return flag;  
    }  
}
